﻿using System.Runtime.InteropServices;
using System.Text;

namespace _58_C_读写ini文件
{
    internal class Program
    {
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string ky, string defalutval, string filePath);
        [DllImport("kernel32")]
        private static extern bool GetPrivateProfileString(string section, string ky, string defalutval, StringBuilder stringbuilder, int size, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string section, string key, int defaultVal, string filePath);

        private static string rootPath = ".\\test.ini";
        static void Main(string[] args)
        {
            //WriteIni();
            //ReadIni();
            //DeleteKey(null);

            Console.WriteLine("运行成功！");
            Console.ReadKey();
        }

        private static void WriteIni()
        {
            WritePrivateProfileString("Student", "Name", "Name1", rootPath);
            WritePrivateProfileString("Student", "Grade", "Grade1", rootPath);
            WritePrivateProfileString("Student", "Age", "18", rootPath);
        }

        private static void ReadIni()
        {
            StringBuilder stringbuilder = new StringBuilder();
            GetPrivateProfileString("Student", "Name", "", stringbuilder, stringbuilder.Capacity, rootPath);
            Console.WriteLine(stringbuilder);

            int age = GetPrivateProfileInt("Student", "Age", 0, rootPath);
            Console.WriteLine(age);
            Console.WriteLine();
        }

        private static void DeleteKey()
        {
            WritePrivateProfileString("Student","Name",null,rootPath);
        }

        private static void DeleteKey1()
        {
            WritePrivateProfileString("Student", null, null, rootPath);
        }
    }

}
